package cn.wolfcode.luowowo.home.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.MytravelQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.home.service.IUserHomeService;
import cn.wolfcode.luowowo.home.vo.UserHomeVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2020/1/9.
 */
@Service
public class UserHomeServiceImpl implements IUserHomeService {
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    //引入操作redis中的用户信息操作
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //引入操作点评的方法
    @Reference
    private IScenicCommentService scenicCommentService;


    @Override
    public UserHomeVO listMyHome(UserInfo userInfo) {
        //查询关注的用户
        List<UserInfo> userFollwos= userInfoService.listByFollow(userInfo.getId());
        //查询关注的用户的数量

        //查询粉丝的数量
        List<UserInfo> userFans = userInfoService.listByFans(userInfo.getId());
        //关联该用户的游记  查询前5篇  按照编辑时间排序
        List<Travel> travels = travelService.list5ForReleaseTimeByUserId(userInfo.getId());
        //根据游记获取目的地与区域的面包屑
        List<Destination> toasts = new ArrayList<>();
        for (Travel travel : travels) {
            //toasts
            toasts = destinationService.getToasts(travel.getDest().getId());
            Collections.reverse(toasts);  //列表反序
            travel.setToasts(toasts);
        }
        //封装进结果集
        UserHomeVO vo = new UserHomeVO();
        //封装面包屑
        vo.setToasts(toasts);
        vo.setFollowUsers(userFollwos);
        vo.setFollowNum(userFollwos.size());
        //查询游记数量
        List<Travel> myAllTravel = travelService.listAllByUserId(userInfo.getId());
        vo.setTravelNum(myAllTravel.size());
        if(userFans==null){ //避免没有粉丝 空指针异常
            vo.setFansNum(0);
        }else{
            vo.setFansNum(userFans.size());//目前只需要粉丝的数量 后续在看情况
        }
        vo.setTravels(travels);
        return vo;
    }

    //根据用户id获得游记分页对象
    @Override
    public List<Travel>  listAllByUserId(Long id) {
        List<Travel> travels = travelService.listAllByUserId(id);
        return travels;
    }

    @Override
    public PageInfo<Travel> queryAllTravelByUserId(MytravelQueryObject qo) {
        return travelService.queryAllTravelByUserId(qo);
    }

    @Override
    public String sendVerity(String phone) throws LogicException {
 /*
          * 1 生成验证码
          * 2 发送验证码
          * 3 手机号码 验证码 发送时间存到session
        * */
        //先看是否有这个手机号码已经注册了

        //先在redis中查询看有无生成的验证码
        String code = stringRedisTemplate.opsForValue().get(RedisKeys.USER_UPDATE_PHONE_CODE_VERIFY.join(phone));
        Boolean hasKey = stringRedisTemplate.hasKey(RedisKeys.USER_UPDATE_PHONE_CODE_VERIFY.join(phone));
        if(hasKey){
            throw new LogicException("验证码已发送,请稍后再试");
        }
        String verityCode = UUID.randomUUID().toString().substring(0, 4);
        //System.out.println(verityCode);
        StringBuilder sb = new StringBuilder();
        sb.append("您的验证码是:").append(verityCode).append(",请在3分钟内使用");
        System.out.println(sb.toString());
        //放进redis中  时效是180秒
        stringRedisTemplate.opsForValue().set(RedisKeys.USER_UPDATE_PHONE_CODE_VERIFY.join(phone),verityCode,180, TimeUnit.SECONDS);
        return sb.toString();
    }

    @Override
    public Boolean checkVerityCode(String phone, String verifyCode) {
        //从redis中查询
        String redisCode = stringRedisTemplate.opsForValue().get(RedisKeys.USER_UPDATE_PHONE_CODE_VERIFY.join(phone));
        if(!verifyCode.equals(redisCode)){
            return false;
        }
        return true;
    }

    @Override
    public List<ScenicComment> setReplayAndThumb(List<ScenicComment> scenic) {
        //获得回复数
        for (ScenicComment scenicComment : scenic) {
            Integer commentNum = scenicCommentService.getCommentNum(scenicComment.getId());
            scenicComment.setReplyNum(commentNum);
        }
        //获得点赞数
        for (ScenicComment scenicComment : scenic) {
            Integer thumbuoNum = scenicCommentService.getThumbuoNum(scenicComment.getId());
            scenicComment.setThumbupnum(thumbuoNum);
        }
        return scenic;
    }

}


